package com.jacky.jpeg;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.signature.ObjectKey;
import com.jacky.log.Logger;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import jacky.utils.BitmapConvert;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    ImageView view;
    String name = "sjpz20210224165817997.jpg";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        view = findViewById(R.id.image);
        findViewById(R.id.btn_90).setOnClickListener(this);
        findViewById(R.id.btn_180).setOnClickListener(this);
        findViewById(R.id.btn_270).setOnClickListener(this);

        copy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_90 :
                loadBitmap(90);
                break;
            case R.id.btn_180 :
                loadBitmap(180);
                break;
            case R.id.btn_270 :
                loadBitmap(270);
                break;
        }
    }

    private void loadBitmap(int r) {
        try {
            InputStream stream = getAssets().open(name);
            ByteArrayOutputStream stream1 = new ByteArrayOutputStream();

            int i = 0;
            byte[] bytes = new byte[1024];
            while (true) {
                i = stream.read(bytes);
                if(i == -1) break;

                stream1.write(bytes, 0, i);
            }
//            stream.close();

            byte[] data = stream1.toByteArray();

            long start = System.currentTimeMillis();
            Bitmap bm  = BitmapFactory.decodeByteArray(data, 0, data.length);
            long end1 = System.currentTimeMillis();
            Logger.e("decode array : ", end1- start);

            BitmapConvert convert =  new BitmapConvert(bm);

            if(r == 90) {
                convert.rotateBitmap90();
            } else if(r == 180) {
                convert.rotateBitmap180();
            } else {
                convert.rotateBitmap270();
            }
            bm = convert.getBitmapAndFree();
            long end2 = System.currentTimeMillis();
            Logger.e("decode array : ", end2- end1);


            File file = new File(getExternalCacheDir(), name);
            file.delete();
            FileOutputStream stream2 = new FileOutputStream(file);
            bm.compress(Bitmap.CompressFormat.JPEG, 90, stream2);

            long end = System.currentTimeMillis();
            Logger.e("decode bitmap : ", end- start);
            Glide.with(this).load(file).apply(
                    new RequestOptions().signature(new ObjectKey(System.nanoTime())))
                    .into(view);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void loadBitmapRenderScript(int r) { //2249
        try {
            InputStream stream = getAssets().open(name);
            ByteArrayOutputStream stream1 = new ByteArrayOutputStream();

            int i = 0;
            byte[] bytes = new byte[1024];
            while (true) {
                i = stream.read(bytes);
                if(i == -1) break;

                stream1.write(bytes, 0, i);
            }
//            stream.close();

            byte[] data = stream1.toByteArray();

            long start = System.currentTimeMillis();
            Bitmap bm  = BitmapFactory.decodeByteArray(data, 0, data.length);
            long end1 = System.currentTimeMillis();
            Logger.e("decode array : ", end1- start);

            if(r == 90) {
                bm = RenderScriptRotation.rotate90(this, bm);
            } else if(r == 180) {
                bm = RenderScriptRotation.rotate180(this, bm);
            } else {
                bm = RenderScriptRotation.rotate270(this, bm);
            }
            long end2 = System.currentTimeMillis();
            Logger.e("decode array : ", end2- end1);


            File file = new File(getExternalCacheDir(), name);
            file.delete();
            FileOutputStream stream2 = new FileOutputStream(file);
            bm.compress(Bitmap.CompressFormat.JPEG, 90, stream2);

            long end = System.currentTimeMillis();
            Logger.e("decode bitmap : ", end- start);
            Glide.with(this).load(file).apply(
                    new RequestOptions().signature(new ObjectKey(System.nanoTime())))
                    .into(view);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void copy() {
        File file = new File(getExternalCacheDir(), name);
        try {
            InputStream stream = getAssets().open(name);
            FileOutputStream stream1 = new FileOutputStream(file);

            int i = 0;
            byte[] bytes = new byte[1024];
            while (true) {
                i = stream.read(bytes);
                if(i == -1) break;

                stream1.write(bytes, 0, i);
            }
            stream.close();
            stream1.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Glide.with(this).load(file).apply(
                new RequestOptions().signature(new ObjectKey(System.nanoTime())))
                .into(view);
    }
}